//
//  MenuView.m
//  zoneSun
//
//  Created by zy-gyl on 16/6/8.
//  Copyright © 2016年 Zeaple. All rights reserved.
//

#import "MenuView.h"

#import "UIView+Extend.h"
#define KColumCount 4
#import "BaseNavigationViewController.h"
#import "FurnitureViewController.h"
#import "BaseViewController.h"
#import "MoreViewController.h"

@implementation MenuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return self;
}

-(void)setDataSoure:(NSMutableArray *)dataSoure{
    _dataSoure = dataSoure;
    [_myCollectionView reloadData];
}
- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //每一行之间的最小距离
    layout.minimumLineSpacing = 0;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =0;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height-2) collectionViewLayout:layout];
    //设置分页
//    collectionV.pagingEnabled = YES;
    [self addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"MenuCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MenuCollectionCell"];
    
    _myCollectionView = collectionV;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSoure.count+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    MenuCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCollectionCell" forIndexPath:indexPath];
    if(indexPath.row==7){
        cell.isMore = YES;
    }else{
        MenuItem *item = self.dataSoure[indexPath.row];
        cell.menuItem = item;
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(Main_Screen_Width/KColumCount, 84);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==7){
        MoreViewController *moreVc = [[MoreViewController alloc]init];
        moreVc.vcName = @"更多";
        moreVc.hidesBottomBarWhenPushed = YES;
        [[self secondNavigationController] pushViewController:moreVc animated:YES];
        return;
    }
    MenuItem *item = self.dataSoure[indexPath.row];
    FurnitureViewController *fvc = [[FurnitureViewController alloc]init];
    fvc.hidesBottomBarWhenPushed = YES;
    fvc.vcName = item.name;
    fvc.tradeId = item.tradeId;
    [[self secondNavigationController] pushViewController:fvc animated:YES];
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end
